// pages/view/dean/dailyDynamics.js
import * as echarts from '../../../../ec-canvas/echarts';
var wxCharts = require('../../../../utils/wxcharts.js');
const network = require('../../../../utils/network.js')
var app = getApp();

var windowWidth = 0;
var lineChart_cl = null;
var lineChart_em = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //查询日期
    regDate: '2018-01-01',
    //门急诊数据-表格
    dailyDynamics:[
        {
        hour: '00',
        gopTimes: '',
        erTimes: ''
        },
      {
        hour: '01',
        gopTimes: 2,
        erTimes: 1
      },
       {
        hour: '02',
        gopTimes: 1,
        erTimes: 0
      }
    ],
    totalTimes:{

    },


    //门急诊数据-图表
    //门诊量
    clTimes: [],
    //急诊量
    emTimes:[],

    ecLine: {
    }

  },
  //点击显示具体数据
  touchHandler_cl: function (e) {
    console.log(lineChart_cl.getCurrentDataIndex(e));
    lineChart_cl.showToolTip(e, {
      // background: '#7cb5ec',
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
  //点击显示具体数据
  touchHandler_em: function (e) {
    console.log(lineChart_em.getCurrentDataIndex(e));
    lineChart_em.showToolTip(e, {
      // background: '#7cb5ec',
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
  //产生时间，0-23时刻
  createSimulationData: function () {
    var categories = [];
    for (var i = 0; i < 24; i++) {
      categories.push('' + i);
    }
    // data[4] = null;
    return {
      categories: categories,
    }
    console.log('categories', categories)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //为加载图表准备
    windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      console.log('window_res',res);
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }
    wx.showLoading({
      title: '加载中',
    })
    
    that.myRequest(options)


    

  },
  /**
   * 调用封装好的网络请求
   */
  myRequest: function (options){
    var that = this;
    //调用封装好的网络请求，获取数据
    network.get('/dean/cldaily?regDate=' + options.startTime, {}
    ).then(function (res) {
      console.log('res:', res)
      var obj = res.data.data.list1
      var obj2 = res.data.data.list2
      var obj3 = res.data.data.list3
      var obj4 = res.data.data.list4

      //console.log('obj2:', obj2)
      that.setData({
        'dailyDynamics': obj,
        'clTimes': obj2,
        'emTimes': obj3,
        'totalTimes': obj4,
        'regDate' : options.startTime
      })
    }).then(function (res) {
      wx.hideLoading()
      //console.log('clTimes111', that.data.clTimes)
      that.getCllineChart()
      //that.getEmlineChart()
      that.setData({
        ecLine: {
          onInit: function (canvas, width, height) {
            const lineChart = echarts.init(canvas, null, {
              width: width,
              height: height
            });
            canvas.setChart(lineChart);
            lineChart.setOption(getLineOption(), true);
            return lineChart;
          }
        }
      })
      console.log('ecLine', that.data.ecLine)
    }).catch(function (res) {
      console.log(res)
    })

  },
  /**
   * 生成门诊折线图
   */
  getCllineChart:function(){
    var that = this;
    var simulationData = that.createSimulationData();
    console.log('simulationData', simulationData)
    lineChart_cl = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: simulationData.categories,
      animation: true,
      // background: '#f5f5f5',
      series: [{
        name: '门诊量',
        //data: [0, 0, 0, 0, 0, 0, 0, 3, 5, 7, 11, 15, 16, 20, 17, 14, 12, 8, 6, 1, 0, 0, 0,0],
        data: that.data.clTimes,
        format: function (val, name) {
          return val + '个';
        }
      },
        {
          name: '急诊量',
          //data: [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 3, 1, 2, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0],
          data: that.data.emTimes,
          format: function (val, name) {
            return val + '个';
          }
        }
      ],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '人数 (个)',
        format: function (val) {
          return val;
        },
        min: 0
      },
      width: windowWidth,
      height: 200,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });

    //return lineChart;

  },

  /**
  * 生成急诊折线图
  */
  getEmlineChart: function () {
    var that = this;
    var simulationData = that.createSimulationData();
    console.log('simulationData', simulationData)
    lineChart_em = new wxCharts({
      canvasId: 'lineCanvas_2',
      type: 'line',
      categories: simulationData.categories,
      animation: true,
      color: '#000',
      //background: '#000',
      series: [ {
        name: '急诊量',
        //data: [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 3, 1, 2, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        data: that.data.emTimes,
        format: function (val, name) {
          return val + '个';
        }
      }
      ],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '人数 (个)'
        
      },
      width: windowWidth,
      height: 200,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });

    

  },

  

});


function getLineOption(canvas, width, height) {
 return {
    title: {
      text: '门急诊量折线图',
      left: 'center'
    },
    color: ["#37A2DA", "#67E0E3", "#9FE6B8"],
    legend: {
      data: ['A', 'B', 'C'],
      top: 50,
      left: 'center',
      backgroundColor: 'red',
      z: 100
    },
    grid: {
      containLabel: false
    },
    tooltip: {
      show: false,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
      // show: false
    },
    yAxis: {
      x: 'center',
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
      // show: false
    },
    series: [{
      name: 'A',
      type: 'line',
      smooth: true,
      data: [5, 8, 15, 20, 17, 12, 3]
    }, {
      name: 'B',
      type: 'line',
      smooth: false,
      data: [0, 0, 1, 3, 7, 0, 0]
    }, {
      name: 'C',
      type: 'line',
      smooth: false,
      data: [0, 0, 1, 2, 0, 0, 0]
    }]
  };
}